package com.example.common.security;

import com.example.common.utils.RequestContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: springsecurity-study
 * @description: 自定义登陆成功处理器
 * @author: ChenZhiXiang
 * @create: 2019-07-28 17:18
 **/
@Component
@Slf4j
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    /***
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @param authentication 其中包含了登录用户的一些相关信息
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                                        Authentication authentication) throws IOException, ServletException {

        log.info("登录成功！");
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","登录成功");
        map.put("auth",authentication);
        RequestContextUtil.writeToResponse(map);
    }
}
